home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / usenet / sources / volume91 / utilitys / sman_1_0 / part01 / src / sless_win.c < prev    next >
C/C++ Source or Header  |  1991-05-18  |  7KB  |  391 lines

  1.  
  2. #include <sless_defs.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <ctype.h>
  6. #include <libraries/dosextens.h>
  7. #include <intuition/intuition.h>
  8. #include <clib/dos_protos.h>
  9. #include <clib/intuition_protos.h>
  10. #include <clib/exec_protos.h>
  11.  
  12. Prototype BPTR            makeoutput(char *);
  13. Prototype void            closeoutput(BPTR);
  14. Prototype LONG            stdpkt(LONG, LONG, BPTR);
  15. Prototype void            setupcon(BPTR);
  16. Prototype void            putconstr(char *, BPTR);
  17. Prototype void            putconchar(char, BPTR);
  18. Prototype short         getconchar(BPTR);
  19. Prototype char            rawreadcon(BPTR);
  20. Prototype struct conreport *    getconreport(short, BPTR);
  21. Prototype struct conreport *    readconreport(BPTR);
  22. Prototype void            windowresized(BPTR);
  23. Prototype char *        getstr(BPTR);
  24. Prototype void            findsize(int *, int *, BPTR);
  25. Prototype void            sto_char(short);
  26. Prototype short         get_sto(void);
  27. Prototype void            putconint(int, BPTR);
  28.  
  29. extern const char memory[];
  30. static char *oldname;
  31.  
  32. extern int sx,sy;
  33. extern char seek;
  34.  
  35. BPTR
  36. makeoutput(n)
  37. char *n;
  38. {
  39.     BPTR con;
  40.     struct MsgPort *cport;
  41.     __aligned struct InfoData id;
  42.     struct Window *win;
  43.     char buff[77];
  44.  
  45.     show_line;
  46.     if (!IsInteractive(con = Output())) {
  47.     strcpy(buff,"con:0/1/640/199/");
  48.     strncat(buff,n,60);
  49.     if (!(con = Open(buff,MODE_NEWFILE)))
  50.         leave(15,"sless: Unable to obtain a display.\n");
  51.     } else {
  52.     stdpkt(ACTION_DISK_INFO,(LONG)(&id >> 2),con);
  53.     win = (struct Window *)(id.id_VolumeNode);
  54.     oldname = win->Title;
  55.     SetWindowTitles(win,n,(char *)-1);
  56.     }
  57.     setupcon(con);
  58.     return con;
  59. }
  60.  
  61. void
  62. closeoutput(con)
  63. BPTR con;
  64. {
  65.     struct MsgPort *cport;
  66.     __aligned struct InfoData id;
  67.     struct Window *win;
  68.  
  69.     show_line;
  70.     if (con != Output()) {
  71.     Close(con);
  72.     return;
  73.     }
  74.  
  75.     stdpkt(ACTION_DISK_INFO,(LONG)(&id >> 2),con);
  76.     win = (struct Window *)(id.id_VolumeNode);
  77.     SetWindowTitles(win,oldname,(char *)-1);
  78.     stdpkt(ACTION_SCREEN_MODE,0,con);
  79.     putconstr("\x9b p",con);
  80. }
  81.  
  82. LONG
  83. stdpkt(type,arg,con)
  84. LONG type,arg;
  85. BPTR con;
  86. {
  87.     __aligned struct StandardPacket pkt;
  88.     struct MsgPort *reply,*who;
  89.  
  90.     show_line;
  91.     who = ((struct FileHandle *)(con << 2))->fh_Type;
  92.  
  93.     if (!(reply = CreatePort(NULL,0)))
  94.     leave(15,"sless: Unable to send packet");
  95.  
  96.     pkt.sp_Msg.mn_Node.ln_Name = (char *)&(pkt.sp_Pkt);
  97.     pkt.sp_Pkt.dp_Link = &(pkt.sp_Msg);
  98.     pkt.sp_Pkt.dp_Port = reply;
  99.     pkt.sp_Pkt.dp_Type = type;
  100.     pkt.sp_Pkt.dp_Arg1 = arg;
  101.     PutMsg(who,&(pkt.sp_Msg));
  102.     WaitPort(reply);
  103.     GetMsg(reply);
  104.  
  105.     DeletePort(reply);
  106.  
  107.     return pkt.sp_Pkt.dp_Res1;
  108. }
  109.  
  110. void
  111. setupcon(con)
  112. BPTR con;
  113. {
  114.     show_line;
  115.     stdpkt(ACTION_SCREEN_MODE,-1,con);
  116.     putconstr("\x9B20h\x9b12{",con);
  117. }
  118.  
  119. struct conreport *
  120. getconreport(e,con)
  121. short e;
  122. BPTR con;
  123. {
  124.     char p;
  125.     struct conreport *cr;
  126.  
  127.     show_line;
  128.     do {
  129.     while ((p = rawreadcon(con)) != (char)0x9B)
  130.         sto_char(p);
  131.  
  132.     if (cr = readconreport(con)) {
  133.         if (cr->conid == e)
  134.         return cr;
  135.         else {
  136.         sto_char(cr->conid);
  137.         free(cr);
  138.         }
  139.     }
  140.     } while (TRUE);
  141. }
  142.  
  143. struct conreport *
  144. readconreport(con)
  145. BPTR con;
  146. {
  147.     struct conreport *cr;
  148.     char p;
  149.     int i=0,v;
  150.  
  151.     show_line;
  152.     if (!(cr = malloc(sizeof(struct conreport))))
  153.     leave(12,memory);
  154.  
  155.     do {
  156.     v = 0;
  157.     p = rawreadcon(con);
  158.     while (isdigit(p)) {
  159.         v = v * 10 + p - '0';
  160.         p = rawreadcon(con);
  161.     }
  162.     cr->args[i++] = v;
  163.     } while (p == ';');
  164.     v = makeconid(p,0);
  165.     if (p == '?' || p == ' ') {
  166.     p = rawreadcon(con);
  167.     v = v + p;
  168.     }
  169.     cr->conid = v;
  170.     if (v == makeconid('|',0) && cr->args[0] == 12) {
  171.     windowresized(con);
  172.     return NULL;
  173.     }
  174.     return cr;
  175. }
  176.  
  177. short
  178. getconchar(con)
  179. BPTR con;
  180. {
  181.     short p;
  182.     struct conreport *cr;
  183.  
  184.     show_line;
  185.     do {
  186.     if (p = get_sto())
  187.         break;
  188.     if ((p = rawreadcon(con)) == (char)'\x9b') {
  189.         if (cr = readconreport(con)) {
  190.         p = cr->conid;
  191.         free(cr);
  192.         break;
  193.         }
  194.     } else
  195.         break;
  196.     } while (TRUE);
  197.     return p;
  198. }
  199.  
  200. char
  201. rawreadcon(con)
  202. BPTR con;
  203. {
  204.     char p;
  205.  
  206.     show_line;
  207.     while (!WaitForChar(con,100000)) {
  208.     chkabort();
  209.     }
  210.     if (Read(con,&p,1) != 1)
  211.     return 0;
  212.     return p;
  213. }
  214.  
  215. void
  216. putconchar(c,con)
  217. char c;
  218. BPTR con;
  219. {
  220.     show_line;
  221.     Write(con,&c,1);
  222.     chkabort();
  223. }
  224.  
  225. void
  226. putconstr(p,con)
  227. char *p;
  228. BPTR con;
  229. {
  230.     show_line;
  231.     Write(con,p,strlen(p));
  232.     chkabort();
  233. }
  234.  
  235. void windowresized(con)
  236. BPTR con;
  237. {
  238.     show_line;
  239.     findsize(&sx,&sy,con);
  240.     if (seek)
  241.     refreshscreen();
  242.     else {
  243.     bottomline();
  244.     putconstr(": ",con);
  245.     }
  246. }
  247.  
  248. char *
  249. getstr(con)
  250. BPTR con;
  251. {
  252.     short c;
  253.     char buff[200],*p = buff,*q;
  254.  
  255.     show_line;
  256.     *p = 0;
  257.     do {
  258.     switch(c = getconchar(con)) {
  259.         case (13) :
  260.         goto end;
  261.         case (makeconid('D',0)) :
  262.         if (p != buff) {
  263.             putconstr("\x9bD",con);
  264.             p--;
  265.         }
  266.         break;
  267.         case (makeconid('C',0)) :
  268.         if (*p) {
  269.             putconstr("\x9bC",con);
  270.             p++;
  271.         }
  272.         break;
  273.         case (makeconid(' ','A')) :
  274.         if (p != buff) {
  275.             putconchar('\x9b',con);
  276.             putconint(p-buff,con);
  277.             putconchar('D',con);
  278.             p = buff;
  279.         }
  280.         break;
  281.         case (makeconid(' ','@')) :
  282.         if (*p) {
  283.             putconchar('\x9b',con);
  284.             putconint(strlen(p),con);
  285.             putconchar('C',con);
  286.             p += strlen(p);
  287.         }
  288.         break;
  289.         case (8) :
  290.         if (p != buff) {
  291.             putconstr("\x9bD\x9bP",con);
  292.             q = --p;
  293.             while (*q = *(q+1))
  294.             q++;
  295.         }
  296.         break;
  297.         case (127) :
  298.         if (*p) {
  299.             putconstr("\x9bP",con);
  300.             q = p;
  301.             while (*q = *(q+1))
  302.             q++;
  303.         }
  304.         default :
  305.         if (isprint(c)) {
  306.             putconstr("\x9b@",con);
  307.             putconchar(c,con);
  308.             q = p + strlen(p) + 1;
  309.             do {
  310.             *q = *(q-1);
  311.             q--;
  312.             } while (q != p);
  313.             *p++ = c;
  314.         }
  315.     }
  316.     } while (TRUE);
  317.  
  318. end:
  319.     if (strlen(buff) == 0)
  320.     return NULL;
  321.     if (!(p = malloc(strlen(buff)+1)))
  322.     leave(12,memory);
  323.     strcpy(p,buff);
  324.     return p;
  325. }
  326.  
  327. void
  328. putconint(x,con)
  329. int x;
  330. BPTR con;
  331. {
  332.     int v;
  333.     char p,f = FALSE;
  334.  
  335.     show_line;
  336.     for (v = 1000000; v > 0; v /= 10) {
  337.     p = x / v;
  338.     x = x - p*v;
  339.     if (p != 0 || f) {
  340.         f = TRUE;
  341.         putconchar(p + '0',con);
  342.     }
  343.     }
  344. }
  345.  
  346. void
  347. findsize(x,y,con)
  348. int *x,*y;
  349. BPTR con;
  350. {
  351.     struct conreport *cr;
  352.  
  353.     show_line;
  354.     putconstr("\x9b0 q",con);
  355.  
  356.     cr = getconreport(makeconid(' ','r'),con);
  357.  
  358.     *x = cr->args[3];
  359.     *y = cr->args[2];
  360.     free(cr);
  361. }
  362.  
  363. #define STO_LEN     100
  364. static short sto_buff[STO_LEN];
  365. static int sto_s = 0,sto_e = 0;
  366.  
  367. void
  368. sto_char(e)
  369. short e;
  370. {
  371.     show_line;
  372.     sto_buff[sto_e] = e;
  373.     sto_e = (sto_e + 1) % STO_LEN;
  374.     if (sto_e == sto_s)             /* lost some off the end */
  375.     sto_s = (sto_s + 1) % STO_LEN;
  376. }
  377.  
  378. short
  379. get_sto()
  380. {
  381.     short c;
  382.  
  383.     show_line;
  384.     if (sto_s == sto_e)
  385.     return 0;
  386.     c = sto_buff[sto_s];
  387.     sto_s = (sto_s + 1) % STO_LEN;
  388.     return c;
  389. }
  390.  
  391.